package com.my.chapter15;

import net.mindview.util.Generator;

public class practice14 {
    public static void main(String[] args) {
        BasicGenerator<CountedObject> gen = new BasicGenerator<>(CountedObject.class);
        for(int i=0;i<6;i++){
            System.out.println(gen.next());
        }
    }
}

class BasicGenerator<T> implements Generator<T>{
    private Class<T> type;
    public BasicGenerator(Class<T> type){
        this.type = type;
    }
    public T next(){
        try {
            return type.newInstance();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    public static <T> Generator<T> create(Class<T> type){
        return new BasicGenerator<T>(type);
    }
}

class CountedObject{
    private static long counter = 0;
    private final long id = ++counter;
    public long id(){
        return id;
    }

    @Override
    public String toString() {
        return "CountedObject" + id ;
    }
}
